String.prototype.format = function(args) {
    var result = this;
    if (arguments.length > 0) {    
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if(args[key]!=undefined){
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
　　　　　　　　　　　　var reg= new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

var by_util = (function(){
    var FROM_IOS_CUSTOMER = 1,
        FROM_WECHAT_WORKER= 2,
        FROM_WEB= 3,
        http = function(o){
            if(!o || typeof(o) !== "object")
                throw "参数必须是非空对象";
            $.ajax({
                 type: o.type,
                 url: o.url+'&callback=jsonpCallback',
                 data: o.data,
                 success: function(data){
                    console.log("success data");
                    o.callback(eval(data));
                 },error:function(error){
                    // console.log(error);
                    // o.callback(error);
                 },
                 dataType: 'jsonp', 
                 jsonp: 'jsonpCallback', 
        });
        window.jsonpCallback = function(data){
            if(o.callback){
                o.callback(data);
            }
        }
    },
    getQueryString = function(name){ 
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        return r?unescape(r[2]):null;
    },
    urlSource = function(){
        return getQueryString('source');
    };

    return {
        httpPost:function(obj){
            obj.type = 'post';
            http(obj);
        },
        httpGet: function(obj){
            obj.type = 'get';
            http(obj);
        },
        PRE_PHOTO_URL:'http://baiying-order.oss-cn-hangzhou.aliyuncs.com',
        PRE_FILE_URL:'http://order-document.oss-cn-hangzhou.aliyuncs.com',

        getQueryString:getQueryString,
        urlSource:urlSource,
        FROM_WECHAT_WORKER:FROM_WECHAT_WORKER,
        FROM_IOS_CUSTOMER:FROM_IOS_CUSTOMER,
        FROM_WEB:FROM_WEB
    };
})($,window);

